var request = require('../../../utils/request.js')
var util = require('../../../utils/util.js')
Page({
  data: {
    cityName: '',
    countyName: '',
    detailInfo: '',
    postalCode: '',
    provinceName: '',
    countryName: '',
    telNumber: '',
    userName: '',
    defaultAddr: false,
    chooseArea: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  /**
   * 省市区选择器
   */
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var region = e.detail.value;
    this.setData({
      chooseArea: true,
      provinceName: region[0],
      cityName: region[1],
      countyName: region[2]
    })
  },
  /**
   * 判断输入的国家是否为中国
   */
  checkCountry: function(e) {
    var that = this;
    var country = e.detail.value;
    that.setData({
      countryName: country,
    })
  },
  //编辑地址　
  formSubmit: function(e) {
    var that = this;
    var province = that.data.provinceName;
    var city = that.data.cityName;
    var district = that.data.countyName;
    var formInfo = e.detail.value;
    var country = util.trimStartAndEnd(formInfo.country);
    var buyerName = util.trimStartAndEnd(formInfo.name);
    var buyerPhoneNum = util.trimStartAndEnd(formInfo.phone);
    var detail = util.trimStartAndEnd(formInfo.detail);
    var postCode = util.trimStartAndEnd(formInfo.postCode);

    if (buyerName == '') {
      wx.showToast({
        title: '收货人姓名不能为空',
        icon: 'none'
      });
      return;
    } else if (buyerPhoneNum == '') {
      wx.showToast({
        title: '收货人电话不能为空',
        icon: 'none'
      });
      return;
    } else if (buyerPhoneNum.length != 11) {
      wx.showToast({
        title: '收货人电话格式错误',
        icon: 'none'
      });
      return;
    } else if (country == '') {
      wx.showToast({
        title: '所在国家不能为空',
        icon: 'none'
      });
      return;
    } else if (detail == '') {
      wx.showToast({
        title: '详细地址不能为空',
        icon: 'none'
      });
      return;
    } else if (postCode == '') {
      wx.showToast({
        title: '邮政编码不能为空',
        icon: 'none'
      });
      return;
    }
    var url = request.URL + "/user/address/add";
    request.request({
      url: url,
      data: {
        userId: 1,
        country: country,
        province: province,
        city: city,
        district: district,
        detail: detail,
        buyerName: buyerName,
        buyerPhoneNum: buyerPhoneNum,
        postcode: postCode,
      },
      success: function(res) {
        console.log(res);
      },
      fail: function() {
        request.showFailInfo()
      }
    })
    wx.navigateBack({

    })

    //console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
})